# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Leo <thinkabit.ukim@gmail.com>
pkgname=fish
pkgver=3.1.2
pkgrel=1
pkgdesc="Modern interactive commandline shell"
url="http://www.fishshell.com"
arch="all"
license="GPL-2.0-only"
depends="bc"
depends_dev="$pkgname-tools"
makedepends="cmake doxygen ncurses-dev ncurses pcre2-dev"
checkdepends="expect bash"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-deinstall"
subpackages="$pkgname-dev $pkgname-doc $pkgname-tools::noarch"
source="https://github.com/fish-shell/fish-shell/releases/download/$pkgver/fish-$pkgver.tar.gz"

prepare() {
	default_prepare
	for f in $(find share/tools -type f -name '*.py'); do
		sed -i -e '1{s@^#!.*@#!%{__python3}@}' "$f"
	done
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B builddir \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		$CMAKE_CROSSOPTS .
	make -C builddir
}

check() {
	cd builddir
	ctest
}

package() {
	make -C builddir install DESTDIR="$pkgdir"
	rm -fr "$pkgdir"/usr/share/$pkgname/groff

	mkdir -p "$pkgdir"/usr/lib
	mv "$pkgdir"/usr/share/pkgconfig "$pkgdir"/usr/lib

	# https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/10159#note_101679
	rm "$pkgdir"/usr/share/fish/completions/rustup.fish
}

doc() {
	default_doc

	mkdir -p "$subpkgdir"/usr/share/$pkgname
	mv "$pkgdir"/usr/share/$pkgname/man "$subpkgdir"/usr/share/$pkgname
}

tools() {
	pkgdesc="$pkgdesc (tools)"
	depends="$pkgname python3 cmd:man"

	mkdir -p "$subpkgdir"/usr/share/$pkgname
	mv "$pkgdir"/usr/share/$pkgname/tools "$subpkgdir"/usr/share/$pkgname
}

sha512sums="b6ae2c928774a2eaccf35312d3a9446bfa3e1335182c8f2b2d6198161d0916904f4964fb20ed13a5bf850c1c819e003905d13db3bc8b1faa5b401a60b47dc563  fish-3.1.2.tar.gz"
